﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Platform.Structure.UserControls
{
    public partial class jErrorWindow : ChildWindow
    {
        public jErrorWindow()
        {
            InitializeComponent();

            this.Loaded += (obj, e) =>
            {
                if (JuCloudy.Framework.Storge.VariableHelper.DEBUG)
                {
                    this._txt.Text = Content;
                    this._debugmode.Visibility = Visibility.Visible;
                    this._releasemode.Visibility = Visibility.Collapsed;
                }
                else
                {
                    this.txt_broswertype.Text = "浏览器内核："+JuCloudy.Framework.Storge.VariableHelper.BrowserName.ToString();
                    this.txt_productversion.Text = "浏览器版本："+JuCloudy.Framework.Storge.VariableHelper.BrowserProductVersion.ToString();
                    this.txt_errormsg.Text = "错误信息："+ErrorMsg.ToString();
                    this.txt_ipaddress.Text = "IP地址："+JuCloudy.Framework.Storge.VariableHelper.IPAddress.ToString();
                    this.txt_localdatetime.Text = "本地时间：" + DateTime.Now.ToString();
                    this.txt_userid.Text = "用户Id：" + JuCloudy.Framework.Storge.VariableHelper.UserId.ToString();
                    this.txt_broswerversion.Text = "内核版本：" + JuCloudy.Framework.Storge.VariableHelper.BrowserVersion.ToString();
                    this.txt_cookiesenable.Text = ((bool)JuCloudy.Framework.Storge.VariableHelper.BrowserCookiesEnabled) ? "Cookie开关：已启启" : "Cookie开关：已禁用";
                    this.txt_platm.Text = "运行平台：" + JuCloudy.Framework.Storge.VariableHelper.BrowserPlatform;
                    this.txt_productname.Text = "浏览器名称：" + JuCloudy.Framework.Storge.VariableHelper.BrowserProductName;
                    this._debugmode.Visibility = Visibility.Collapsed;
                    this._releasemode.Visibility = Visibility.Visible;
                }
            };
        }

        public new String Content { get; set; }

        public String ErrorMsg { get; set; }

        public Exception Error { get; set; }

        private void ChildWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            JuCloudy.Platform.Structure.MainPage homepage
                = Application.Current.RootVisual as JuCloudy.Platform.Structure.MainPage;

            homepage.IsEnabled = true;
        }

        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void jButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btn_submit_Click(object sender, RoutedEventArgs e)
        {
            this.mask_loading.Visibility = Visibility.Visible;
            SystemErrorLog.SystemErrorLogSoapClient client =
                new SystemErrorLog.SystemErrorLogSoapClient();
            client.InsertCompleted += (obj, ex) =>
            {
                this.mask_loading.Visibility = Visibility.Collapsed;
                if (ex.Error != null)
                    throw ex.Error;
                
            };

            client.InsertAsync(new JuCloudy.Data.Entity.SysErrorLog()
            {
                BrowserInfo = JuCloudy.Framework.Storge.VariableHelper.BrowserInfo.Serialize(),
                Details = this.Error.Serialize(),
                ErrorMsg = this.ErrorMsg,
                ReportDateTime = DateTime.Now,
                ReportDateTimeStamp = 0,
                ReporterId = (JuCloudy.Framework.Storge.VariableHelper.UserId == null) ? 0 : (int)JuCloudy.Framework.Storge.VariableHelper.UserId,
                ReporterIpAddress = JuCloudy.Framework.Storge.VariableHelper.IPAddress
            }.Serialize());
        }


    }
}

